package com.duke.shaking.more;

import java.util.HashMap;
import java.util.Map;

import android.view.View;
import android.widget.EditText;

import com.duke.shaking.R;
import com.duke.shaking.ResultCodeFactory;
import com.duke.shaking.TitleActivity;
import com.duke.shaking.net.AsyncHttpResponseWithResultHandler;
import com.duke.shaking.net.NetUtils;
import com.duke.shaking.utils.PrefUtils;
import com.loopj.android.http.AsyncHttpClient;

public class ChangePasswordActivity extends TitleActivity {

	private String newPwd;
	@Override
	protected int getLayoutId() {
		// TODO Auto-generated method stub
		return R.layout.activity_change_password;
	}

	@Override
	protected void onTitleInit() {
		// TODO Auto-generated method stub
		initTitle(TITLE_BTN_EXIST | TITLE_BTN_BACK,
				R.string.back,
				0,
				0,
				R.string.title_change_password);
	}
	
	public void onSaveNewPassword(View v) {
		EditText editNewPwd = (EditText)this.findViewById(R.id.edit_newpwd);
		EditText reEditNewPwd = (EditText)this.findViewById(R.id.edit_repeat_newpwd);
		
		newPwd = editNewPwd.getText().toString();
		String reNewPwd = reEditNewPwd.getText().toString();
		
		if("".equals(newPwd.trim()) || "".equals(reNewPwd.trim())) {
			toast(R.string.err_empty_password);
			return;
		}
		
		if(!newPwd.equals(reNewPwd)) {
			toast(R.string.err_password_not_same);
			return;
		}
 		
		AsyncHttpResponseWithResultHandler responseHandler = new AsyncHttpResponseWithResultHandler(){
			
        	@Override
			public void onResultSuccess(String response) {
				super.onResultSuccess(response);
				toast(R.string.change_password_successfully);
				afterSaveSuccess();
			}

			@Override
			public void onResultFailure(String result) {
				
				super.onResultFailure(result);
				if (ResultCodeFactory.REQUEST_OLDPSW_ERROR.equals(result)) {
					toast(R.string.err_old_password);
				}
				else if (ResultCodeFactory.REQUEST_NEWPSW_ERROR.equals(result)) {
					toast(R.string.err_new_password);
				}
				else {
					toast(R.string.err_change_password);
				}
				
			}
			
			@Override
			public void onFailure(Throwable arg0, String arg1) {
				super.onFailure(arg0, arg1);
				toast(R.string.err_network_issues);
			}
        };
        
        Map<String, String> params = new HashMap<String, String>();
        params.put(NetUtils.OLDPSW, me.getPassword());
        params.put(NetUtils.NEWPSW, newPwd);
        params.put(NetUtils.TOKEN, me.getAuthToken());
        AsyncHttpClient client = NetUtils.changePassword(this, responseHandler, params);
        if (client != null) {
        	showProgress(R.string.progress_change_password, client, responseHandler);
        }
	}
	
	private void afterSaveSuccess() {
		PrefUtils prefUtils = PrefUtils.getInstance(this, this.getString(R.string.registerPref));
		
		String saved = prefUtils.getString(this.getString(R.string.savedPassword), null);
		if (saved != null) {
			prefUtils.addString(this.getString(R.string.savedPassword), newPwd);
		}
		this.finish();
	}
}
